package chabernac.android.wordraster.rastersolver;

import java.util.Random;

public class RandomLetterHelper {
	private static final int[] VOUALS = new int[]{'A'-'A','E'-'A','I'-'A','O'-'A','U'-'A'};
  private final Random myRandom = new Random();
  private final int[] myLetterOccurance;
  private int myTotalLetterOccurance;
  private int myVoualOccurance; 
  private float myVoualDistribution;
  

  public RandomLetterHelper(){
    myLetterOccurance = new int[26];
  }
  
  public RandomLetterHelper(int[] aLetterOccurance) {
    super();
    myLetterOccurance = aLetterOccurance;
    calculateTotalLetterOccurance();
  }
  
  public void calculateTotalLetterOccurance(){
  	myTotalLetterOccurance = 0;
    for(int i=0;i<myLetterOccurance.length;i++){
      myTotalLetterOccurance += myLetterOccurance[i];
    }
    
    myVoualOccurance = 0;
    for(int i=0;i<VOUALS.length;i++){
    	myVoualOccurance += myLetterOccurance[VOUALS[i]];
    }
    
    myVoualDistribution = myVoualOccurance / myTotalLetterOccurance;
  }
  
  public char getRandomLetter(){
    int theRandomNumber = Math.abs(myRandom.nextInt() % myTotalLetterOccurance);
    
    int theCounter = 0;
    for(int i=0;i<myLetterOccurance.length;i++){
      theCounter += myLetterOccurance[i];
      if(theRandomNumber < theCounter) return (char)('A' + i);
    }
    return 'x';
  }
  
  public void addWord(String aWord){
    for(char theLetter : aWord.toCharArray()){
      if(theLetter >= 'A' && theLetter <='Z'){
        myLetterOccurance[theLetter - 'A']++;
      }
    }
  }
  
  

}
